package sample.reflects;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

class Person2 {
	private String name;
	private int age;

	public Person2() {

	}

	public Person2(String name) {
		this.name = name;
	}

	public Person2(int age) {
		this.age = age;
	}

	public Person2(String name,int age){
		this.age=age;
		this.name=name;
	}
	public String getName() {
		return name;
	}

	//must be shield
//	public void setName(String name) {
//		this.name = name;
//	}

	public int getAge() {
		return age;
	}

//	public void setAge(int age) {
//		this.age = age;
//	}

	public String toString() {
		return "[" + this.name + " " + this.age + "]";
	}

}

public class HelloPerson2 {
	
	public static void main(String[]args){
		Class<?> demo=null;
		
		try {
			demo=Class.forName("sample.reflects.Person2");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Person2 per1=null;
		Person2 per2=null;
		Person2 per3=null;
		Person2 per4=null;
		
		Constructor<?> cons[] = demo.getConstructors();
		try {
			per1=(Person2) cons[0].newInstance();
			per2=(Person2) cons[1].newInstance("anqing");
			per3=(Person2) cons[2].newInstance(1000);
			per4=(Person2) cons[3].newInstance("anqing",1000);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}

		System.out.println(per1);
		System.out.println(per2);
		System.out.println(per3);
		System.out.println(per4);
	}

}
